#include "log_api.h"
#include "memroy_app.h"
#include "temporary_file.h"
#include "cardholder_api.h"
#include "device_parameter.h"

/**
 * @brief 更新设备参数回调
 *
 */
static void UpdateDeviceParameterCallback(void);

static void UpdateDeviceParameterCallback(void)
{
  bool UpdateResult = WriteDeviceParametersToMemory();
  PRINT("[system]: update decice parametes result: %d.\r\n", UpdateResult);
}

void MemoryUpdateDeviceParameters(void)
{
  MemoryDelayedStartUpdateParameterTask(DELAY_IN_UPDATE_DEVICE_PARAMETERS);
}

void MemoryUpdateDeviceParametersNoWait(void)
{
  MemoryStartUpdateParameterTaskNoWait();
}

void MemoryLoadDeviceParametersFromMemory(void)
{
  LoadDeviceParameterFromMemory();
}

uint16_t MemoryWriteUnlockLogMessage(const uint8_t KeyType, const uint8_t *pKeyValueIn, uint8_t Status)
{
  uint16_t WriteTableId = 0;

  if (GetDeviceInitStatus() & NORMAL_MODE)
  {
    switch (KeyType)
    {
    case LOG_PHYSICAL_CARD_NUMBER:
      WriteTableId = WritePhysicalCardNumberLog(pKeyValueIn, LOG_DATA_PACKET_LENGTH, Status);
      PRINT("WriteTableId %0d \r\n", WriteTableId);
      break;

    case LOG_USER_KEYSCANF_PASSWORD:
      WriteTableId = WriteKeyboardScanfLog(pKeyValueIn, LOG_DATA_PACKET_LENGTH, Status);
      break;

    case LOG_FINGER_SCANF:
      WriteTableId = WriteFingerprintVerificationLog(pKeyValueIn, LOG_DATA_PACKET_LENGTH, Status);
      break;

    case LOG_FACE_SCANF:
      WriteTableId = WriteFaceVerificationLog(pKeyValueIn, LOG_DATA_PACKET_LENGTH, Status);
      break;

    case LOG_QR_CODE_SCANF:
      WriteTableId = WriteQrcodeUnlockExecuteResultLog(pKeyValueIn, LOG_DATA_PACKET_LENGTH, Status);
      break;

    case LOG_COMMAND_OPEN_DOOR:
      WriteTableId = WriteCommandUnlockExecuteResultLog(pKeyValueIn, LOG_DATA_PACKET_LENGTH, Status);
      break;

    case LOG_BLUETOOTH_VIRTUAL_CARD_NUMBER:
      WriteTableId = WriteBluetoothVirtualCardOpenDoorLog(pKeyValueIn, LOG_DATA_PACKET_LENGTH, Status);
      break;

    case LOG_EXIT_BUTTON:
      WriteTableId = WriteExitButtonUnlockLog();
      break;
    }
  }

  return WriteTableId;
}


bool MemoryInitialisingdeviceparameters(void)
{
  bool DeviceInitializationStatus = false;

  LoadDeviceParameterFromMemory();
  PRINT("[system]: load user configuration data.\r\n");

  uint8_t DeviceStatus = GetDeviceInitStatus();
  switch (DeviceStatus)
  {
  case NORMAL_MODE:
  case FACTORY_SETTING:
  case (NORMAL_MODE | FIRMWARE_UPGRADE):
  case (FACTORY_SETTING | FIRMWARE_UPGRADE):
  {
    if (DeviceStatus & FIRMWARE_UPGRADE)
    {
      PRINT("[system]: remove the ota logo.\r\n");

      SetDeviceInitStatus(DeviceStatus & (~FIRMWARE_UPGRADE));

      MemoryUpdateDeviceParameters();
    }

    {
      PRINT("[system]: compare firmware version numbers.\r\n");
      char FirmwareVersionNumber[NUMBER_OF_FIRMWARE_VERSION] = {0};

      GetDeviceFirmwareVersion(FirmwareVersionNumber);
      if (strcmp(FirmwareVersionNumber, DEFAULT_FIRMWARE_VERSION) != 0)
      {
        PRINT("[system]: inconsistent firmware version numbers, new version: %s.\r\n", DEFAULT_FIRMWARE_VERSION);

        SetDeviceFirmwareVersion(DEFAULT_FIRMWARE_VERSION);
        MemoryUpdateDeviceParameters();
      }
    }
  }
  break;

  default:
  {
    PRINT("[system]: initialization system profile.\r\n");

    InitialisationLogFile();
    DeleteAllTemporaryFile();
    InitialisingDeviceParameters();
    InitialiseCardholderDirectory();
    MemoryUpdateDeviceParameters();

    DeviceInitializationStatus = true;
  }
  break;
  }

  return DeviceInitializationStatus;
}

void MemoryConfiguartion(void)
{
  MemoryExternalInterfaces_t ExternalInterfaces = {0};

  ExternalInterfaces.UpdateDeviceParmaeterTask = UpdateDeviceParameterCallback;
  MemoryBoardInitialisation(&ExternalInterfaces);
}
